package com.sgb.kjwl.viewmodel.packers;

import android.text.TextUtils;
import android.util.Log;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.request.PackersProjectTeamListEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.packers.PackersProjectTeamFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*@author kobe577590
*@date 2020/6/11
*description:
*/
public class ProjectTeamFragmentModel {

    private HomeRepertory repertory;
    private PackersProjectTeamFragment fragment;

    public ProjectTeamFragmentModel(PackersProjectTeamFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getList(String firstCategoryNo,String secondCategoryNo,String thirdCategoryNo,String isCompany,String teamSize, String teamStatus,int page) {
        Map<String,Object> map = new HashMap<>();
        map.put("areacode", repertory.getAreaCode());
        if(!TextUtils.isEmpty(isCompany) && !"-1".equals(isCompany)){
            map.put("isCompany", isCompany);
        }
        if(!TextUtils.isEmpty(teamStatus) && !"-1".equals(teamStatus)){
            map.put("teamStatus", teamStatus);
        }
        map.put("limit", 8);
        map.put("page", page);
        if(!TextUtils.isEmpty(teamSize) && !"0".equals(teamSize)){
            map.put("teamSize", teamSize);
        }
        if(!TextUtils.isEmpty(firstCategoryNo)){
            map.put("firstCategoryNo", firstCategoryNo);
        }
        if(!TextUtils.isEmpty(secondCategoryNo)){
            map.put("secondCategoryNo", secondCategoryNo);
        }
        if(!TextUtils.isEmpty(thirdCategoryNo)){
            if(!"0".equals(thirdCategoryNo)){
                map.put("thirdCategoryNo", thirdCategoryNo);
            }
        }

        DialogHelper.showProgressDialog(fragment.getActivity(),null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getProjectTeamList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<PackersProjectTeamListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<PackersProjectTeamListEntity>>>> call, Response<BaseEntity<ProListEntity<List<PackersProjectTeamListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if ( null == response.body().getData()) {
                    return;
                }
                fragment.showList(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<PackersProjectTeamListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void getFilterTypeData(String id) {
        APIManager.getInstance().getPackerAPI().getSecondCategory(id).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTypeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    public void getScaleData() {
        APIManager.getInstance().getPackerAPI().getTeamScale(repertory.getAreaCode(),"1").enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    Log.v("scal",response.body().toString());
                    fragment.setScalDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    public void getH5Url(String id){
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("constructteamdetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id+ "&type=1";
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
